/*
 * Created by JFormDesigner on Mon May 27 13:16:46 CST 2024
 */

package view.room;

import java.awt.*;
import common.RoomState;
import common.RoomTypes;
import model.room.Room;

import javax.swing.*;
import java.awt.event.ActionEvent;

/**
 * @author 孟晗
 */
public class AddRoom extends JFrame {


    private RoomTableModel roomTableModel;

    public AddRoom(RoomTableModel roomTableModel) {
        this.roomTableModel = roomTableModel;
        initComponents();

    }


   /* private void button1(ActionEvent e) {
        String id=textField1.getText();
        double price= Double.parseDouble(textField2.getText());
        RoomTypes Type= RoomTypes.valueOf(textField3.getText());
        RoomState State=RoomState.valueOf(textField4.getText());

        Room stl=new Room(id,price,State,Type);
        stl.setRoomNumber(id);
        stl.setPrice(price);
        stl.setRoomTypes(Type);
        stl.setRoomState(State);

            boolean asd=roomTableModel.addRoom(stl);
            if (asd) {
                JOptionPane.showMessageDialog(this, "添加成功");
                this.dispose();
                roomTableModel.fireTableDataChanged();
            }else {
                JOptionPane.showMessageDialog(this, "添加失败,请检查格式");
                this.dispose();
            }

    }*/
   private void button1(ActionEvent e) {

       try {
           String id=textField1.getText();
           double price = Double.parseDouble(textField2.getText());
           RoomTypes Type = RoomTypes.valueOf(textField3.getText());
           RoomState State = RoomState.valueOf(textField4.getText());

           if (id.isEmpty() || price <= 0 || Type == null || State == null) {
               JOptionPane.showMessageDialog(this, "输入格式有误，请检查后重试");
               return;
           }
           if (Room.getRooms().stream().anyMatch(room -> room.getRoomNumber().equals(id))) {
               JOptionPane.showMessageDialog(this, "房间号已存在，请重新输入");
               return;
           }
           Room stl = new Room(id, price, State, Type);
           stl.setRoomNumber(id);
           stl.setPrice(price);
           stl.setRoomTypes(Type);
           stl.setRoomState(State);

           boolean asd = roomTableModel.addRoom(stl);
           if (asd) {
               JOptionPane.showMessageDialog(this, "添加成功");
               this.dispose();
               roomTableModel.fireTableDataChanged();
           } else {
               JOptionPane.showMessageDialog(this, "添加失败,请检查房间号是否已存在");
           }
       } catch (IllegalArgumentException ex) {
           JOptionPane.showMessageDialog(this, "输入格式有误，请检查后重试");
       }

   }

   /* private void fireTableDataChanged() {
        roomTableModel.fireTableDataChanged();
        roomTableModel.list();
    }*/


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - mh
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        textField1 = new JTextField();
        textField2 = new JTextField();
        textField3 = new JTextField();
        textField4 = new JTextField();
        button1 = new JButton();

        //======== this ========
        var contentPane = getContentPane();

        //---- label1 ----
        label1.setText("  \u623f\u95f4\u53f7\uff1a");

        //---- label2 ----
        label2.setText("    \u8d39\u7528\uff1a");

        //---- label3 ----
        label3.setText("    \u7c7b\u578b\uff1a");

        //---- label4 ----
        label4.setText("    \u72b6\u6001\uff1a");

        //---- button1 ----
        button1.setText("\u6dfb\u52a0");
        button1.addActionListener(e -> button1(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(36, 36, 36)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                .addComponent(label2, GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                                .addComponent(label1, GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                                .addComponent(label3, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                                .addComponent(label4, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                .addComponent(textField1, GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                                .addComponent(textField2, GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                                .addComponent(textField3, GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                                .addComponent(textField4, GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(162, 162, 162)
                            .addComponent(button1)))
                    .addContainerGap(105, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(27, 27, 27)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label3, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(textField4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label4, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(button1)
                    .addContainerGap(54, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - mh
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField4;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
